import request from '@/utils/request'

// CRM相关接口

// 客户管理接口
export const getCustomerList = (params: {
  page?: number
  pageSize?: number
  customerName?: string
  phone?: string
  status?: string
  intentionLevel?: string
  userId?: string
  startDate?: string
  endDate?: string
}) => {
  return request({
    url: '/api/crm/customers',
    method: 'get',
    params
  })
}

export const getCustomerDetail = (id: string) => {
  return request({
    url: `/api/crm/customers/${id}`,
    method: 'get'
  })
}

export const createCustomer = (data: any) => {
  return request({
    url: '/api/crm/customers',
    method: 'post',
    data
  })
}

export const updateCustomer = (id: string, data: any) => {
  return request({
    url: `/api/crm/customers/${id}`,
    method: 'put',
    data
  })
}

export const deleteCustomer = (id: string) => {
  return request({
    url: `/api/crm/customers/${id}`,
    method: 'delete'
  })
}

export const batchDeleteCustomers = (ids: string[]) => {
  return request({
    url: '/api/crm/customers/batch-delete',
    method: 'delete',
    data: { ids }
  })
}

export const transferCustomer = (data: {
  customerIds: string[]
  fromUserId: string
  toUserId: string
  reason?: string
}) => {
  return request({
    url: '/api/crm/customers/transfer',
    method: 'post',
    data
  })
}

export const markCustomerInvalid = (data: {
  customerIds: string[]
  reason: string
  remark?: string
}) => {
  return request({
    url: '/api/crm/customers/mark-invalid',
    method: 'post',
    data
  })
}

export const exportCustomers = (params: any) => {
  return request({
    url: '/api/crm/customers/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 跟进记录接口
export const getFollowUpList = (params: {
  page?: number
  pageSize?: number
  customerId?: string
  userId?: string
  followUpMethod?: string
  intentionLevel?: string
  startDate?: string
  endDate?: string
}) => {
  return request({
    url: '/api/crm/follow-ups',
    method: 'get',
    params
  })
}

export const getFollowUpDetail = (id: string) => {
  return request({
    url: `/api/crm/follow-ups/${id}`,
    method: 'get'
  })
}

export const createFollowUp = (data: any) => {
  return request({
    url: '/api/crm/follow-ups',
    method: 'post',
    data
  })
}

export const updateFollowUp = (id: string, data: any) => {
  return request({
    url: `/api/crm/follow-ups/${id}`,
    method: 'put',
    data
  })
}

export const deleteFollowUp = (id: string) => {
  return request({
    url: `/api/crm/follow-ups/${id}`,
    method: 'delete'
  })
}

export const getFollowUpReminders = () => {
  return request({
    url: '/api/crm/follow-ups/reminders',
    method: 'get'
  })
}

export const getFollowUpAnalytics = (params: {
  startDate?: string
  endDate?: string
  userId?: string
}) => {
  return request({
    url: '/api/crm/follow-ups/analytics',
    method: 'get',
    params
  })
}

// 分配记录接口
export const getAllocationList = (params: {
  page?: number
  pageSize?: number
  customerId?: string
  fromUserId?: string
  toUserId?: string
  startDate?: string
  endDate?: string
}) => {
  return request({
    url: '/api/crm/allocations',
    method: 'get',
    params
  })
}

export const createAllocation = (data: {
  customerIds: string[]
  toUserId: string
  reason?: string
  remark?: string
}) => {
  return request({
    url: '/api/crm/allocations',
    method: 'post',
    data
  })
}

export const getAllocationAnalytics = (params: {
  startDate?: string
  endDate?: string
}) => {
  return request({
    url: '/api/crm/allocations/analytics',
    method: 'get',
    params
  })
}

// 客户转移接口
export const getTransferList = (params: {
  pageNum?: number
  pageSize?: number
  customerName?: string
  transferStatus?: string
  applicantName?: string
  applyDateRange?: string[]
}) => {
  return request({
    url: '/api/crm/transfers',
    method: 'get',
    params
  })
}

export const approveTransfer = (data: {
  transferId: string
  remark?: string
}) => {
  return request({
    url: '/api/crm/transfers/approve',
    method: 'post',
    data
  })
}

export const rejectTransfer = (data: {
  transferId: string
  remark?: string
}) => {
  return request({
    url: '/api/crm/transfers/reject',
    method: 'post',
    data
  })
}

export const batchApproveTransfer = (ids: string[]) => {
  return request({
    url: '/api/crm/transfers/batch-approve',
    method: 'post',
    data: { ids }
  })
}

export const batchRejectTransfer = (ids: string[]) => {
  return request({
    url: '/api/crm/transfers/batch-reject',
    method: 'post',
    data: { ids }
  })
}

export const deleteTransfer = (id: string) => {
  return request({
    url: `/api/crm/transfers/${id}`,
    method: 'delete'
  })
}

export const exportTransfers = (params: any) => {
  return request({
    url: '/api/crm/transfers/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

export const batchTransferCustomer = (data: {
  customerIds: string[]
  targetUserId: string
  remark?: string
  requireApproval?: boolean
}) => {
  return request({
    url: '/api/crm/transfers/batch',
    method: 'post',
    data
  })
}

export const getUserList = () => {
  return request({
    url: '/api/system/users',
    method: 'get'
  })
}

// 配置接口
export const getFollowUpMethods = () => {
  return request({
    url: '/api/crm/config/follow-up-methods',
    method: 'get'
  })
}

export const updateFollowUpMethods = (methods: string[]) => {
  return request({
    url: '/api/crm/config/follow-up-methods',
    method: 'put',
    data: { methods }
  })
}

export const getIntentionLevels = () => {
  return request({
    url: '/api/crm/config/intention-levels',
    method: 'get'
  })
}

export const updateIntentionLevels = (levels: any[]) => {
  return request({
    url: '/api/crm/config/intention-levels',
    method: 'put',
    data: { levels }
  })
}

// 客户状态变更记录
export const getCustomerStatusHistory = (customerId: string, params?: {
  pageNum?: number
  pageSize?: number
}) => {
  return request({
    url: `/api/crm/customers/${customerId}/status-history`,
    method: 'get',
    params
  })
}

// 客户标签管理
export const getCustomerTags = (customerId: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/tags`,
    method: 'get'
  })
}

export const addCustomerTag = (customerId: string, tag: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/tags`,
    method: 'post',
    data: { tag }
  })
}

export const removeCustomerTag = (customerId: string, tag: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/tags/${tag}`,
    method: 'delete'
  })
}

// 客户备注管理
export const getCustomerRemarks = (customerId: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/remarks`,
    method: 'get'
  })
}

export const addCustomerRemark = (customerId: string, content: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/remarks`,
    method: 'post',
    data: { content }
  })
}

export const updateCustomerRemark = (customerId: string, remarkId: string, content: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/remarks/${remarkId}`,
    method: 'put',
    data: { content }
  })
}

export const deleteCustomerRemark = (customerId: string, remarkId: string) => {
  return request({
    url: `/api/crm/customers/${customerId}/remarks/${remarkId}`,
    method: 'delete'
  })
}